﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hisuntech.SystemFramework.Net.Mail;
using System.IO;

namespace MailClient
{
    public partial class Form1 : Form
    {
        private bool bindGridData;
        private string AttachmentPath;
      
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSendMail_Click(object sender, EventArgs e)
        {
            if (this.txtMailTo.Text != "" && this.txtMailSubject.Text != "")
            {
                MailServerInfo server = MailConfig.GetConfig(MailServerType.SMTP);
                Emails curMail = IBMP.IOC.Unity.Instance.GetInstance<Emails>();// 使用这种方式即可发送多种邮件
                curMail.SMTP_Server = server;

                MailMessageInfo msgInfo = new MailMessageInfo();
                msgInfo.MailFrom = server.ServerUsername;
                msgInfo.MailTo = this.txtMailTo.Text;
                msgInfo.MsgSubject = this.txtMailSubject.Text;
                msgInfo.MsgContent = this.txtMailContent.Text;
                //msgInfo.AddAttachment(fileName);

                bool result = curMail.Send(msgInfo);
                if (result)
                    MessageBox.Show("发送成功！");
                else
                    MessageBox.Show("发送邮件失败：" + curMail.ErrorMessage);
            }
            else
            {
                MessageBox.Show("收件人或者标题不能为空！");
            }
            

        }

        private void btnReceivMail_Click(object sender, EventArgs e)
        {
            bindGridData = false;
            this.lblReceivMsg.Text = "正在连接邮件服务器...";
            Application.DoEvents();

            MailServerInfo server = MailConfig.GetConfig(MailServerType.POP3);
            Emails curMail = IBMP.IOC.Unity.Instance.GetInstance<Emails>();// 使用这种方式即可发送多种邮件
            curMail.POP3_Server  = server;
            curMail.ReceivingHandler += new EventHandler<WcfMail.SystemMessageEventArgs>(curMail_ReceivingHandler);
            curMail.ReceivedHandler += new EventHandler<WcfMail.SystemMessageEventArgs>(curMail_ReceivedHandler);
            if (curMail.Receive(new WcfMail.SystemMessage()))
            {
                List<MailMessageInfo> msgList = curMail.MessageQueue.ConvertAll<MailMessageInfo>(
                    new Converter<WcfMail.Interface.ISysMsg, MailMessageInfo>
                        (p => (MailMessageInfo)p)
                        );
                this.grvMailList.AutoGenerateColumns = false;
                this.grvMailList.DataSource = msgList;
                bindGridData = true;
                /*
                msgList[0].MessageID;
                msgList[0].MsgSubject;
                msgList[0].MsgContent;
                msgList[0].MailFrom;
                 */ 
                
            }
            else
            {
                MessageBox.Show(curMail.ErrorMessage);
            }
            AttachmentPath = curMail.AttachmentPath;
        }

        void curMail_ReceivedHandler(object sender, WcfMail.SystemMessageEventArgs e)
        {
            this.lblReceivMsg.Text ="完成 "+ e.Message.MsgSubject;
            Application.DoEvents();
        }

        void curMail_ReceivingHandler(object sender, WcfMail.SystemMessageEventArgs e)
        {
            this.lblReceivMsg.Text ="正在接收 "+ e.Message.MsgSubject;
            Application.DoEvents();
        }

        private void grvMailList_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (bindGridData)
            {
                if (e.RowIndex>=0)
                {
                    MailMessageInfo msg = (MailMessageInfo)this.grvMailList.Rows[e.RowIndex ].DataBoundItem;
                    this.txtMailMsgContent.Text = msg.MsgContent;
                    this.cmbAttachment.DataSource = msg.AttachmentFiles;
                   
                }
                
            }
        }

        private void btnSeeMailContent_Click(object sender, EventArgs e)
        {
            string strFile = this.AttachmentPath +"/tempEmailBody.htm";
            System.Text.Encoding encoding = this.txtMailMsgContent.Text.IndexOf("gb2312",StringComparison.OrdinalIgnoreCase ) > 0 ? System.Text.Encoding.GetEncoding("gb2312") : System.Text.Encoding.UTF8;
            byte[] bytContent = encoding.GetBytes(this.txtMailMsgContent.Text);
            if (File.Exists(strFile))
                File.Delete(strFile);
            FileStream fs = File.Create(strFile);
            fs.Write(bytContent, 0, bytContent.Length);
            fs.Flush();
            fs.Close();

            string fullPath=System.IO.Path.GetFullPath(strFile);
            System.Diagnostics.Process.Start(fullPath);
        }

        private void btnOpenAttachement_Click(object sender, EventArgs e)
        {
            if (cmbAttachment.Text != "")
            {
                string fullPath = System.IO.Path.GetFullPath(cmbAttachment.Text);
                System.Diagnostics.Process.Start(fullPath);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MailServerInfo popServer= MailConfig.GetConfig(MailServerType.POP3);
            MailServerInfo smtpServer = MailConfig.GetConfig(MailServerType.SMTP );

            this.txtSMTP_MailPort.Text = smtpServer.MailPort.ToString ();
            this.txtSMTP_MailServer.Text = smtpServer.MailServer;
            this.txtSMTP_ServerPassword.Text = smtpServer.ServerPassword;
            this.txtSMTP_ServerUsername.Text = smtpServer.ServerUsername;

            this.txtPOP3_MailPort.Text = popServer.MailPort.ToString();
            this.txtPOP3_MailServer.Text = popServer.MailServer;
            this.txtPOP3_ServerPassword.Text = popServer.ServerPassword;
            this.txtPOP3_ServerUsername.Text = popServer.ServerUsername;

        }

        private void btnSaveAuthCfg_Click(object sender, EventArgs e)
        {
            MessageBox.Show("请直接修改应用程序配置文件！"); 
        }
    }
}
